/*
 * StdChatWindowModule.cpp
 *
 *  Created on: 09.08.2009
 *      Author: h3llfire
 */

#include "StdChatWindowModule.h"
#include "../../core/Impp.h"

namespace Impp {

IMPLEMENT_DYNAMIC_CLASS(StdChatWindowModule, iChatWindow);

StdChatWindowModule::StdChatWindowModule() :
	mode(CHAT_WINDOW_USE_TABS),
	buttonList(ChatPanel::ID_CHATPANEL_HIGHEST){


}

StdChatWindowModule::~StdChatWindowModule() {

}

void StdChatWindowModule::moduleInit(){
	//create BuddyMenuEntry
	iUserInterface* ui = Impp::exemplar()->getModuleManager()->getUserInterfaceModule();
	if(ui != NULL){
		ui->reigsterBuddyMenuEntryAdder(STD_CHATWINDOW_MODULE_NAME, StdChatWindowModule::addShowChatWindowToBuddyMenu);
	}else{
		wxLogDebug(wxT("ChatWindow::moduleInit: No UserInterface found to add Buddy Menu entry!"));
	}

	Impp* impp = Impp::exemplar();
	impp->Connect(EVT_MESSAGE_IN,MessageEventHandler(StdChatWindowModule::onMessageIn), NULL, this);
	impp->Connect(EVT_TYPING_NOTIFICATION,TypingNotificationEventHandler(StdChatWindowModule::onTypingNotification), NULL, this);
	impp->Connect(EVT_IMPP_COMMAND,ImppCommandEventHandler(StdChatWindowModule::onImppCommand), NULL, this);

}

void StdChatWindowModule::moduleDestroy(){
	std::vector<ChatWindow*>::iterator i;
	for(i = windows.begin(); i != windows.end(); i++){
		ChatWindow* win = *i;
		delete win;
	}
}

void StdChatWindowModule::showChatWindow(Buddy* buddy){
	ChatWindow* win = getWindowFor(buddy);
	win->showChatWindow(buddy);
}

ChatWindow* StdChatWindowModule::getWindowFor(Buddy* buddy){
	//Lookup if Buddy is opened in a ChatwindowTab
	std::vector<ChatWindow*>::iterator i;
	for(i = windows.begin(); i != windows.end(); i++){
		ChatWindow* win = *i;
		if(win->hasBuddy(buddy)){
			return win;
		}
	}
	//Buddy not opened so decide by mode:
	if(mode == CHAT_WINDOW_USE_TABS){
		if(windows.size() == 0){
			ChatWindow* win = new ChatWindow(this);
			windows.push_back(win);
			return win;
		}else{
			return windows[0];
		}
	}else if(mode == CHAT_WINDOW_NO_TABS){
		ChatWindow* win = new ChatWindow(this);
		windows.push_back(win);
		return win;
	}
	//should never happen
	return NULL;
}

void StdChatWindowModule::addActionButtion(const wxString & moduleName, const wxString & actionName, const wxString & title, const wxString & description){
	buttonList.addActionButton(moduleName,actionName,title,description);
}

void StdChatWindowModule::onMessageIn(MessageEvent & event){
	ChatWindow* win = getWindowFor(event.getBuddy());
	win->onMessageIn(event);
}
void StdChatWindowModule::onTypingNotification(TypingNotificationEvent & event){
	ChatWindow* win = getWindowFor(event.getSender());
	win->onTypingNotification(event);
}

void wxCALLBACK StdChatWindowModule::addShowChatWindowToBuddyMenu(iMenu* menu, Buddy* userData){
	menu->appendItem(STD_CHATWINDOW_MODULE_ACTION_SHOW_WINDOW, wxT("Send Message"), 10);
}

void StdChatWindowModule::onImppCommand(ImppCommandEvent & event){
	if(event.getModuleName() == STD_CHATWINDOW_MODULE_NAME){
		if(event.getActionName() == STD_CHATWINDOW_MODULE_ACTION_SHOW_WINDOW){
			Buddy* buddy = event.getBuddy();
			wxASSERT(buddy != NULL);
			showChatWindow(buddy);
		}
	}else{
		event.Skip(true);
	}
}

ButtonList* StdChatWindowModule::getButtonList(){
	return &buttonList;
}

}
